﻿using ComponentFactory.Krypton.Toolkit;
using SRB.Tg.Model;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Teigha.DatabaseServices;

namespace SRB.Tg.Cad
{
    public partial class PileInfoForm : KryptonForm
    {
        DataRow _row;
        double _scale;

        private static PileInfoForm _instance;
        public static PileInfoForm getInstance(DataRow row, double scale)
        {
            if (_instance == null)
            {
                _instance = new PileInfoForm(row, scale);
            }
            return _instance;
        }
        public static PileInfoForm getInstance()
        {
            if (_instance == null)
            {
                _instance = new PileInfoForm();
            }
            return _instance;
        }
        public PileInfoForm()
        {
            InitializeComponent();
        }
        public PileInfoForm(DataRow row,double scale)
        {
            InitializeComponent();
            _row = row;
            _scale = scale;
            this.configure_name.Text = row["configure_name"].ToString();
            this.acoustic_tube_number.Text = row["acoustic_tube_number"].ToString();
            this.acoustic_tube_type.Text = row["acoustic_tube_type"].ToString();
            this.bottom_distance.Text = row["bottom_distance"].ToString();
            this.bottom_encryption_length.Text = row["bottom_encryption_length"].ToString();
            this.bottom_encryption_spacing.Text = row["bottom_encryption_spacing"].ToString();
            this.bottom_size.Text = row["bottom_size"].ToString();
            this.concrete_no.Text = row["concrete_no"].ToString();
            this.cross_section.Text = row["cross_section"].ToString();
            this.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();
            this.connector_spacing.Text = row["connector_spacing"].ToString();
            this.inner_strengthen_reinforcement_diameter.Text = row["inner_strengthen_reinforcement_diameter"].ToString();
            this.inner_strengthen_reinforcement_spacing.Text = row["inner_strengthen_reinforcement_spacing"].ToString();
            this.main_reinforcement_diameter.Text = row["main_reinforcement_diameter"].ToString();
            this.main_reinforcement_number.Text = row["main_reinforcement_number"].ToString();
            this.material_type.Text = row["material_type"].ToString();
            this.outer_strengthen_reinforcement_diameter.Text = row["outer_strengthen_reinforcement_diameter"].ToString();
            this.outer_strengthen_reinforcement_spacing.Text = row["outer_strengthen_reinforcement_spacing"].ToString();
            this.pile_diameter.Text = row["pile_diameter"].ToString();
            this.pile_length.Text = row["pile_length"].ToString();
            this.pile_penetration_depth.Text = row["pile_penetration_depth"].ToString();
            this.reinforcement_grade.Text = row["reinforcement_grade"].ToString();
            this.spiral_reinforcement_diameter.Text = row["spiral_reinforcement_diameter"].ToString();
            this.standard_spacing.Text = row["standard_spacing"].ToString();
            this.structure_number.Text = row["structure_number"].ToString();
            this.structure_type.Text = row["structure_type"].ToString();
            this.top_encryption_length.Text = row["top_encryption_length"].ToString();
            this.top_encryption_spacing.Text = row["top_encryption_spacing"].ToString();
            this.top_reinforcement_diameter.Text = row["top_reinforcement_diameter"].ToString();
            this.top_reinforcement_number.Text = row["top_reinforcement_number"].ToString();
            this.top_size.Text = row["top_size"].ToString();
        }

        public void SavModify()
        {
            if (_row != null)
            {
                if (!_row.IsNull("id"))
                {
                    string updatesql = string.Format("update t_pile_reinforcement set acoustic_tube_number='{1}'," 
                        + "acoustic_tube_type='{2}',bottom_distance='{3}',bottom_encryption_length='{4}',bottom_encryption_spacing='{5}',"
                        + "bottom_size='{6}',concrete_no='{7}',concrete_protect_layer='{8}',connector_spacing='{9}',"
                        + "inner_strengthen_reinforcement_diameter='{10}',inner_strengthen_reinforcement_spacing='{11}',main_reinforcement_diameter='{12}',main_reinforcement_number='{13}',"
                        + "outer_strengthen_reinforcement_diameter='{14}',outer_strengthen_reinforcement_spacing='{15}',pile_diameter='{16}',pile_length='{17}',"
                        + "pile_penetration_depth='{18}',reinforcement_grade='{19}',spiral_reinforcement_diameter='{20}',standard_spacing='{21}',"
                        + "top_encryption_length='{22}',top_encryption_spacing='{23}',top_reinforcement_diameter='{24}',top_reinforcement_number='{25}',top_size='{26}'"
                        + " where id='{0}'", _row["id"].ToString(),
                        this.acoustic_tube_number.Text, this.acoustic_tube_type.Text, this.bottom_distance.Text,
                       this.bottom_encryption_length.Text, this.bottom_encryption_spacing.Text,this.bottom_size.Text,
                       this.concrete_no.Text,this.concrete_protect_layer.Text,this.connector_spacing.Text,
                       this.inner_strengthen_reinforcement_diameter.Text,this.inner_strengthen_reinforcement_spacing.Text,
                       this.main_reinforcement_diameter.Text,this.main_reinforcement_number.Text,this.outer_strengthen_reinforcement_diameter.Text,
                       this.outer_strengthen_reinforcement_spacing.Text,this.pile_diameter.Text,this.pile_length.Text,
                       this.pile_penetration_depth.Text,this.reinforcement_grade.Text,this.spiral_reinforcement_diameter.Text,
                       this.standard_spacing.Text,this.top_encryption_length.Text,this.top_encryption_spacing.Text,
                       this.top_reinforcement_diameter.Text,this.top_reinforcement_number.Text,this.top_size.Text);
                    //暂不更新，算量计算后期和服务端关联
                    MySqlHelper.ExecuteNonQuery(updatesql, CommandType.Text);
                    PileParameter para = ModelOper.GetPilePara(_row["id"].ToString(), out _row);
                    // 钢筋明细
                    string selectString = string.Format("SELECT * FROM t_reinforcement_summary where part_type='{0}' and parent_id='{1}' and status=0  order by no asc", 2, _row["id"].ToString());
                    System.Data.DataTable dt1 = MySqlHelper.ExecuteDataTable(selectString, System.Data.CommandType.Text);

                    // 材料明细
                    selectString = string.Format("SELECT * FROM t_material_summary where part_type='{0}' and parent_id='{1}' and calculate_type='{2}' and status=0 ", 2, _row["id"].ToString(), 1);
                    System.Data.DataTable dt2 = MySqlHelper.ExecuteDataTable(selectString, System.Data.CommandType.Text);

                    int OverAllScale = (int)_scale;
                    Pile P1 = new Pile(para);
                    Database db = HostApplicationServices.WorkingDatabase;
                    P1.DrawingPile(db, OverAllScale, dt1, dt2);
                }
            }
        }
    }
}
